package cn.tx.util;

import cn.tx.dao.BooksDao;
import cn.tx.dao.GradeDao;
import cn.tx.model.Books;
import cn.tx.model.Grade;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RunWith(SpringJUnit4ClassRunner.class)// 添加spring测试方案
@ContextConfiguration("/config/spring/applicationContext.xml")// 指定spring配置文件位置
public class MyBatisTest {

    @Autowired
    private GradeDao gradeDao;
    @Autowired
    private BooksDao booksDao;

    //
    @Test
    public void testInsert() {
        Grade c = new Grade();
        c.setName("2022");
        c.setYname("测试组");
        gradeDao.addGrade(c);
    }
    @Test
    public void testUpdate(){
        Books c = new Books();
        c.setName("2222");
        c.setAuthor("111");

        String strDate = "2021-07-12";
        /*
        SimpleDateFormat lsdStrFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d1 = lsdStrFormat.parse(strDate);
            c.setDatetime(d1);
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        booksDao.addBooks(c);
    }
    @Test
    public void testfenye(){

        Map<String,Object> querymap = new HashMap<>();
        querymap.put("name","%人机设计%");
        querymap.put("offset", 0);
        querymap.put("pageSize", 2);
        List<Books> list = booksDao.findList(querymap);
        int total = booksDao.findTotal(querymap);
      //  System.out.println(total);
        for(Books c:list){
            System.out.println(c.getId()+"---"+c.getName());
        }
    }
    @Test
    public void testDelete() {
        gradeDao.delete("27");
    }
    @Test
    public void testSelect() {
        List<Grade> list =  gradeDao.findAll();
        for(Grade c:list){
            System.out.println(c.getYname()+"---"+c.getName());
        }
    }

}
